define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 获取URL中的user_id和teacher_detail_id参数
            var urlParams = new URLSearchParams(location.search);
            var user_id = urlParams.get('user_id');
            var teacher_detail_id = urlParams.get('teacher_detail_id');
            var addUrl = 'teacher_course_display/add';
            var params = [];
            if (user_id) {
                params.push('user_id=' + user_id);
            }
            if (teacher_detail_id) {
                params.push('teacher_detail_id=' + teacher_detail_id);
            }
            if (params.length > 0) {
                addUrl += '?' + params.join('&');
            }
            
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'teacher_course_display/index' + location.search,
                    add_url: addUrl,
                    edit_url: 'teacher_course_display/edit',
                    del_url: 'teacher_course_display/del',
                    multi_url: 'teacher_course_display/multi',
                    import_url: 'teacher_course_display/import',
                    table: 'teacher_course_display',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'user.nickname',
                            title: __('User'),
                            operate: 'LIKE',
                            halign: "center",
                            searchable: false,
                            formatter: Table.api.formatter.content
                        },
                        {field: 'title', title: __('Title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'img', title: __('Img'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'sort', title: __('Sort')},
                        {field: 'addtime', title: __('Addtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
